#include <soar.h>

int main()
{
    int res;
    CNFG cnfg;
    struct cnfg_property *array;
    int array_num;
    struct cnfg_property property;

    if((res=cnfg_load("sample.cnfg", &cnfg)))
    {
        printf("cnfg_load: 出错，错误码:%d\n", res);
        if(res > 0)
            printf("第%d行出错\n", res);
        return(-1);
    }

    /*----- 全局配置 -----*/
    printf("[GLOBAL]\n");
    if((array_num=cnfg_property_array(cnfg, NULL, 0, &array)) < 0)
    {
        printf("cnfg_entry_array(GLOBAL)出错，错误码:%d\n", array_num);
        goto OUT_ERR;
    }
    if(array_num > 0)
    {
        int i;

        for(i=0; i<array_num; i++)
        {
            printf("%s", array[i].name);
            if(array[i].type != CNFG_PROPERTY_SIMPLE)
            {
                if(array[i].value)
                    printf(" = %s", array[i].value);
            }
            printf("\n");
        }
        free(array);
    }
    printf("\n");

    /*----- SERVER段配置 -----*/
    printf("[SERVER]\n");
    if((res=cnfg_get_property(cnfg, "SERVER", "COMMON_ARG", 0,
                    &property)))
    {
        printf("cnfg_get_property(SERVER:COMMON_ARG)出错，错误码:%d\n",
                res);
        goto OUT_ERR;
    }
    printf("%s", property.name);
    if(property.value)
        printf("=%s\n", property.value);
    printf("大小写敏感: ");
    if((res=cnfg_get_property(cnfg, "SERVER", "Common_Arg",
                    FLAG_CASE_SENSITIVE, &property)))
    {
        if(res == RET_ERR_CNFG_PROPERTY_NOT_FOUND)
            printf("Common_Arg未找到\n");
        else
            printf("cnfg_get_property(SERVER:Common_Arg)出错，"
                    "错误码:%d\n",
                    res);
    }
    printf("\n");

    printf("[SERVER]所有条目\n");
    if((array_num=cnfg_property_array(cnfg, "SERVER", 0, &array)) < 0)
    {
        printf("cnfg_property_array(SERVER)出错，错误码:%d\n", array_num);
        goto OUT_ERR;
    }
    if(array_num > 0)
    {
        int i;

        for(i=0; i<array_num; i++)
        {
            printf("%s", array[i].name);
            if(array[i].type == CNFG_PROPERTY_COMPOSITE)
            {
                printf("(复合条目) ");
                cnfg_get_subproperty(cnfg, "SERVER", array[i].name,
                        "MIN", 0, &property);
                printf("MIN=%s ",
                        BUF_IS_CLR(property.value)?
                        "[EMPTY]":property.value); 
                cnfg_get_subproperty(cnfg, "SERVER", array[i].name,
                        "ID", 0, &property);
                printf("ID=%s ",
                        BUF_IS_CLR(property.value)?
                        "[EMPTY]":property.value); 
                cnfg_get_subproperty(cnfg, "SERVER", array[i].name,
                        "ARG", 0, &property);
                printf("ARG=%s ",
                        BUF_IS_CLR(property.value)?
                        "[EMPTY]":property.value); 
            }
            else
            {
                printf("(单一条目) ");
                if(array[i].value)
                    printf("=%s", array[i].value);
            }
            printf("\n");
        }
        free(array);
    }
    printf("\n");

    cnfg_free(cnfg);
    return(0);

OUT_ERR:
    cnfg_free(cnfg);
    return(-1);
}
